import logging

from django.db import transaction
from django.db.models.signals import post_delete
from django.dispatch import receiver

from member.models import BonusPoint
from wxchat.api.utils import get_user_by_openid

logger = logging.getLogger("django")


# @receiver(post_delete, sender=BonusPoint)
# def client_bonus_point_cancel(sender, instance, **kwargs):
#     logging.info("Enter client_bonus_point_cancel")
#     if instance:
#         try:
#             retailer = instance.retailer
#             customer_openid = instance.customer_openid
#             customer = get_user_by_openid(customer_openid)
#             if retailer and customer:
#                 with transaction.atomic():
#                     save_id = transaction.savepoint()
#                     try:
#                         # 商家积分增加
#                         retailer.score += instance.score + instance.unused_score
#                         retailer.save(update_fields=["score"])
#                         # 客户积分减少
#                         customer.score -= instance.score
#                         customer.unused_score -= instance.unused_score
#                         customer.save(update_fields=["score", "unused_score"])
#                         print(customer.score, customer.unused_score)
#                     except Exception as ex:
#                         print(ex)
#                         transaction.savepoint_rollback(save_id)
#
#                     transaction.savepoint_commit(save_id)
#         except Exception as e:
#             logging.error("Exception:{}".format(e))
#     logging.info("Leave client_bonus_point_cancel")


